package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<Address> findList() {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId, UserHolder.get().getId());
        return addressMapper.selectList(wrapper);
    }

    @Override
    public void add(Address address) {
        //1.填充字段
        address.setUserId(UserHolder.get().getId());
        address.setIsDefault(0);//设置不是默认地址
        addressMapper.insert(address);
    }

    @Override
    public void setdefault(Long id) {
        //1.找到当前用户的所有地址，设置非默认
        //update address_book set is_default = 0 where user_id = 登录用户的id

        //按照条件更新:参数一：更新之后的对象  参数二：条件
        //1.1构建更新对象
        Address address = new Address();
        address.setIsDefault(0);
        //1.2构造更新条件
        LambdaUpdateWrapper<Address> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Address::getUserId, UserHolder.get().getId());
        //1.3执行更新
        addressMapper.update(address, wrapper);

        //2.根据id将指定的一个地址设置默认地址
        //update address_book set is_default = 1 where user_id = 传入的id
        Address address1 = new Address();
        address1.setIsDefault(1);
        address1.setId(id);
        addressMapper.updateById(address1);

    }

    @Override
    public Address findDefault() {
        //设置查询条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId, UserHolder.get().getId())
                .eq(Address::getIsDefault, 1);
        return addressMapper.selectOne(wrapper);
    }

    @Override
    public Address findById(Long id) {
        return addressMapper.selectById(id);
    }

    @Override
    public void update(Address address) {
        addressMapper.updateById(address);
    }

    @Override
    @Transactional
    public void deleteByIds(long[] ids) {
        //判断要删除的地址和是否是默认地址
        for (long id : ids) {
            LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Address::getId, id);
            wrapper.eq(Address::getIsDefault, 1);

            Integer count = addressMapper.selectCount(wrapper);
            if (count > 0) {
                throw new CustomException("要删除的地址为默认地址，无法删除");
            } else {
                addressMapper.deleteById(id);
            }
        }
    }

}
